﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iOak.Core.DataExchange;
using System.Data;
using System.Transactions;
using iOak.Web;
using iOak.Domain.Campaigns;
using iOak.Business.Campaigns;
using iOak.Business.Resumes;
using iOak.Domain.Resumes;

namespace iOak.Web.ExeclHelper
{
    public class ImportManager
    {
        private static readonly ApplicationCandidateService itemApplicationCandidateService = new ApplicationCandidateService();
        private static readonly ResumeDetailService itemResumeDetailService = new ResumeDetailService();
        private static readonly ResumeService itemResumeService = new ResumeService();
        private static readonly WorkAssistant workassistant = new WorkAssistant();

        public static string ImportCheck(string filePath, List<ResumeField> allList)
        {
            string strCheck = string.Empty;
            using (ExcelHelper excelHelper = new ExcelHelper(filePath))
            {
                excelHelper.Hdr = "YES";
                excelHelper.Imex = "1";

                DataTable dt = excelHelper.ReadTable("Sheet1");

                foreach (DataRow dr in dt.Rows)
                {
                    var listResumeField = allList.Where(m => m.Reqiured == true).ToList();
                    foreach (var item in listResumeField)
                    {
                        if (dt.Columns.Contains(item.LabelText))
                        {
                            string value = Convert.ToString(dr[item.LabelText]);
                            if (item.Reqiured)
                            {
                                if (string.IsNullOrWhiteSpace(value))
                                {
                                    strCheck = "必须输入:" + item.LabelText;
                                    return strCheck;
                                }
                            }
                            if (item.MaxLength != null)
                            {
                                int length = value.Length;
                                if (length > item.MaxLength)
                                {
                                    int row = dt.Rows.IndexOf(dr) + 1;
                                    int col = dt.Columns.IndexOf(item.LabelText) + 1;
                                    strCheck = item.LabelText + "  第" + row + "行 第" + col + "列，超出限定长度！";
                                    return strCheck;
                                }
                            }

                            // 正则表达式判断
                        }
                    }
                }
            }

            return strCheck;
        }

        public static void ImportFromXls(string filePath, Guid resumrTemplateId, Guid applicationId, List<ResumeField> allList)
        {
            using (ExcelHelper excelHelper = new ExcelHelper(filePath))
            {
                excelHelper.Hdr = "YES";
                excelHelper.Imex = "1";

                DataTable dt = excelHelper.ReadTable("Sheet1");
                using (TransactionScope ts = new TransactionScope())
                {
                    var listResumeField = allList.Where(m => m.Reqiured == true).ToList();
                    // 计算出比例
                    double proportion = Math.Round(listResumeField.Count * 1.0 / allList.Count * 100);
                    foreach (DataRow dr in dt.Rows)
                    {
                        Guid resumeId = Guid.NewGuid();

                        ApplicationCandidate addItem = new ApplicationCandidate();
                        addItem.ApplicationCandidateId = Guid.NewGuid();
                        addItem.ApplicationId = applicationId;
                        addItem.CheckedBy = workassistant.CurrentUser.UserId;
                        addItem.CheckerOn = null;
                        addItem.Notes = string.Empty;
                        addItem.Passed = null;
                        addItem.ResumeId = resumeId;
                        addItem.Score = null;
                        itemApplicationCandidateService.Create(addItem);

                        Resume resumeItem = new Resume();
                        resumeItem.Completeness = (int)proportion;
                        resumeItem.CreatedBy = workassistant.CurrentUser.UserId;
                        resumeItem.CreatedOn = DateTime.Now;
                        resumeItem.ModifiedBy = workassistant.CurrentUser.UserId;
                        resumeItem.ModifiedOn = DateTime.Now;
                        resumeItem.ResumeId = resumeId;
                        resumeItem.ResumeTemplateId = resumrTemplateId;
                        itemResumeService.Create(resumeItem);

                        listResumeField.ForEach(item =>
                            {
                                if (dt.Columns.Contains(item.LabelText))
                                {
                                    var itemResumeDetail = new ResumeDetail();
                                    itemResumeDetail.Key = item.FieldName;
                                    itemResumeDetail.ResumeDetailId = Guid.NewGuid();
                                    itemResumeDetail.ResumeId = resumeId;
                                    if (item.ValueType.Equals("DateTime"))
                                    {
                                        itemResumeDetail.ValueString = Convert.ToDateTime(dr[item.LabelText]).ToString(item.ValueFormat);
                                    }
                                    else
                                    {
                                        itemResumeDetail.ValueString = Convert.ToString(dr[item.LabelText]);
                                    }
                                    itemResumeDetailService.Create(itemResumeDetail);
                                }
                            });

                    }
                    ts.Complete();
                }
            }
        }
    }
}
